using MediatR;
using SumerCoreDevOps.Application.Permissions.Commands;
using SumerCoreDevOps.Application.Permissions.Queries;
using SumerCoreDevOps.Domain.Entities;
using SumerCoreDevOps.Infrastructure.Identity;

namespace SumerCoreDevOps.Application.Permissions.Handlers;

public class CreatePermissionCommandHandler : IRequestHandler<CreatePermissionCommand, Permission>
{
    private readonly PermissionManager _permissionManager;

    public CreatePermissionCommandHandler(PermissionManager permissionManager)
    {
        _permissionManager = permissionManager;
    }

    public async Task<Permission> Handle(CreatePermissionCommand request, CancellationToken cancellationToken)
    {
        return await _permissionManager.CreatePermissionAsync(
            request.PermissionCode,
            request.PermissionName,
            request.Description,
            request.Category,
            request.ResourceType,
            request.ResourceIdentifier,
            request.SortOrder,
            request.CreatedBy
        );
    }
}

public class DeletePermissionCommandHandler : IRequestHandler<DeletePermissionCommand>
{
    private readonly PermissionManager _permissionManager;

    public DeletePermissionCommandHandler(PermissionManager permissionManager)
    {
        _permissionManager = permissionManager;
    }

    public async Task Handle(DeletePermissionCommand request, CancellationToken cancellationToken)
    {
        await _permissionManager.DeletePermissionAsync(request.PermissionId);
    }
}

public class GetPermissionByIdQueryHandler : IRequestHandler<GetPermissionByIdQuery, Permission?>
{
    private readonly PermissionManager _permissionManager;

    public GetPermissionByIdQueryHandler(PermissionManager permissionManager)
    {
        _permissionManager = permissionManager;
    }

    public async Task<Permission?> Handle(GetPermissionByIdQuery request, CancellationToken cancellationToken)
    {
        return await _permissionManager.GetPermissionByIdAsync(request.PermissionId);
    }
}

public class GetAllPermissionsQueryHandler : IRequestHandler<GetAllPermissionsQuery, List<Permission>>
{
    private readonly PermissionManager _permissionManager;

    public GetAllPermissionsQueryHandler(PermissionManager permissionManager)
    {
        _permissionManager = permissionManager;
    }

    public async Task<List<Permission>> Handle(GetAllPermissionsQuery request, CancellationToken cancellationToken)
    {
        return await _permissionManager.GetAllPermissionsAsync();
    }
}

public class GetPermissionsByCategoryQueryHandler : IRequestHandler<GetPermissionsByCategoryQuery, List<Permission>>
{
    private readonly PermissionManager _permissionManager;

    public GetPermissionsByCategoryQueryHandler(PermissionManager permissionManager)
    {
        _permissionManager = permissionManager;
    }

    public async Task<List<Permission>> Handle(GetPermissionsByCategoryQuery request, CancellationToken cancellationToken)
    {
        return await _permissionManager.GetPermissionsByCategoryAsync(request.Category);
    }
}

public class CheckUserPermissionQueryHandler : IRequestHandler<CheckUserPermissionQuery, bool>
{
    private readonly PermissionManager _permissionManager;

    public CheckUserPermissionQueryHandler(PermissionManager permissionManager)
    {
        _permissionManager = permissionManager;
    }

    public async Task<bool> Handle(CheckUserPermissionQuery request, CancellationToken cancellationToken)
    {
        return await _permissionManager.CheckUserPermissionAsync(request.UserId, request.PermissionCode);
    }
}
